package com.example.demo;

import com.example.vo.User;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.Scalars;
import graphql.schema.*;

public class GraphQLDemo {
    /**
     * Auther sun
     * DATE 2022/4/29 15:25
     * VERSION 1.0
     */

    public static void main(String[] args) {
        GraphQLObjectType userObjectType = GraphQLObjectType.newObject()
                .name("User")
                .field(GraphQLFieldDefinition.newFieldDefinition().name("id").type(Scalars.GraphQLLong))
                .field(GraphQLFieldDefinition.newFieldDefinition().name("name").type(Scalars.GraphQLString))
                .field(GraphQLFieldDefinition.newFieldDefinition().name("age").type(Scalars.GraphQLInt))
                .build();

        GraphQLObjectType userQueryObjectType = GraphQLObjectType.newObject()
                .name("UserQuery")
                .field(GraphQLFieldDefinition.newFieldDefinition().name("user").type(userObjectType)
                        .argument(GraphQLArgument.newArgument().name("id").type(Scalars.GraphQLLong).build())
                        .dataFetcher(environment -> {
                            Long id = environment.getArgument("id");
                            return new User(id, "东方", 20 + id.intValue());
                        }))
                .build();

        GraphQLSchema graphQLSchema = GraphQLSchema.newSchema()
                .query(userQueryObjectType)
                .build();

        GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build();

        String query = "{user(id:1){id,name,age}}";
        ExecutionResult result = graphQL.execute(query);

        //标准反馈
        System.out.println(result.toSpecification());
        System.out.println(result.getData().toString());


    }
}
